
using LF.Application.Services.interfaces;
using LF.Domain.Entities;
using LF.Domain.Services;
using LF.Application.Services.Iml;
using LF.Shared.Dtos.Out;
using LF.Shared.Dtos.Input;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;

namespace LF.Application.Services.Iml
{
    [Authorize]
    [ApiExplorerSettings(GroupName = SwaggerGroup.Sys)]
    public class UserDeptService: BaseService, IUserDeptService
    {
        IUserDeptManagerService _manager;
        public UserDeptService(IUserDeptManagerService manager,
            IObjectMapper<LFModule> mapper,
            IGlobalManagerService globalManager):base(mapper,globalManager)
        {
            _manager = manager;
        }

        public Result<UserDeptSearchOut> UserDepts(UserDeptSearch search)
        {
            var result = _manager.All(search.Index, search.PageSize, search.Key);
            var list = _mapper.Map<List<UserDept>, List<UserDeptOut>>(result);
            var count = _manager.Count(search.Key);
            return Ok(new UserDeptSearchOut { Items = list, TotalCount = count });
        }

        public Result RealDelete(Guid id)
        {
            if (_globalManager.RealDelete<UserDept>(id))
                return Ok();
            else
                return Error("删除失败!");
        }

        public Result<UserDeptOut> Get(Guid id)
        {
            var item = _globalManager.Get<UserDept>(id);
            var result = _mapper.Map<UserDept, UserDeptOut>(item);
            return Ok(result);
        }

        public Result Edit(UserDeptOut userDept)
        {
            bool result;
            var model = _mapper.Map<UserDeptOut, UserDept>(userDept);
            if (userDept.Id == null)
            {
                model.Id = Guid.NewGuid();
                result = _globalManager.Insert(model);
            }
            else
            {
                result = _globalManager.Update(model);
            }
            if (result)
                return Ok();
            else
                return Error("保存失败!");
        }

        [HttpPost]
        public Result Delete(Guid[] ids)
        {
            if (ids?.Length < 1)
            {
                return Error("请选择");
            }
            var result = _globalManager.DeleteByWhere<UserDept>(p => ids!.Contains(p.Id));
            if (result)
                return Ok();
            else
                return Error("删除失败!");
        }
    }
}
